<?php
/**
 * Default Controller
 * @package    sitrack 
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright (c) 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Default controller.
 *
 * This controller handles the dashboard and admin pages.
 *
 * @final 
 * @package    sitrack 
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright (c) 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
class Default_Controller extends SPF_Controller {

   /**
    * Constructor
    * @return   object
    */
   public function __construct() {
      /* construct the parent object */
      parent::__construct(__CLASS__);
   }

   /**
    * Destructor
    * @return   void
    */
   public function __destruct() {
      parent::__destruct();
   }

   /**
    * Determines if the client is permitted to access this controller and perform the requested action.
    * Access to this controller requires a signed-in user.    
    *
    * @return  boolean   true if access is allowed, false otherwise.
    */
   public function authenticate() {
   
      return (SPF::$session->user != NULL);
      
   } // authenticate
   
   /**
    * Dashboard page.
    * 
    * @param   string    $filter   the filter used to show records.
    * @return  void.
    */
   public function index( $filter = NULL ) {
      
      switch( $filter ) {
         
         case 'assigned_me':
         case 'created_me':
         case 'created_today':
         case 'updated_today':
            break;
            
         default:
            $filter = 'assigned_me';
            break;
            
      } // switch
      
      $this->assign('filter', $filter);
      $this->assign('tickets', Ticket::dashboard_data($filter));
      $this->assign('projects', Project::browse_data());
      
      $this->display('index');
      
   } // index
   
   /**
    * Admin page.
    * 
    * @return  void.
    */
   public function admin() {
      
      $this->assign('project_count', SPF::$db->get_count('projects'));
      
      $this->assign('ticket_count', SPF::$db->get_count('tickets'));
      $this->assign('ticket_data', Ticket::get_count_by_status(true));
      
      $this->assign('user_count', SPF::$db->get_count('users'));
      $this->assign('user_data', User::get_count_by_status());
            
      $this->display('admin');
      
   
   } // admin
   
   public function http_test() {
      
      $http = new SPF_HTTP();
      
      $url = 'http://sitrack.localhost/http.php';
      $data = array('username' => 'simon', 'password' => 'abc123');
      
      $r = $http->post($url, $data);
      
      echo "<pre>\n";
      echo "------------------------------\n";
      echo $r->get_body();
      echo "------------------------------\n";
      echo '</pre>';	
   }
   
   public function email_test() {
      
      $email = new SPF_Email();
      
      $email->subject = 'Hello World';
      $email->body = 'Test';
      $email->add_recipient('simon@simondownes.co.uk');
      
      $email->send();
      
      d($email);
      
   }
   
} // Default_Controller

?>
